package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.bo.zy.WXOrderCommentBO;
import com.cskaoyan.bean.bo.zy.WXOrderListBO;
import com.cskaoyan.bean.bo.zy.WXOrderSubmitBO;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.common.Constant;
import com.cskaoyan.bean.common.ZYBaseVO;
import com.cskaoyan.bean.marketBean.MarketOrderGoods;
import com.cskaoyan.bean.marketBean.MarketUser;
import com.cskaoyan.bean.vo.zy.WXOrderDetailVO;
import com.cskaoyan.bean.vo.zy.WXOrderListVO;
import com.cskaoyan.bean.vo.zy.WXOrderSubmitVO;
import com.cskaoyan.service.zy.WXOrderModuleZYService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 订单模块
 *
 * @author zhangYun
 * @since 2022/12/31 11:50
 */
@RestController
@RequestMapping("wx/order")
public class WXOrderModuleZYController {
    @Autowired
    private WXOrderModuleZYService wxOrderModuleZYService;

    /**
     * http://182.92.235.201:8083/wx/order/list?showType=4&page=1&limit=10 GET
     *
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo orderList(WXOrderListBO parama) {
        CommonData<WXOrderListVO> result = new CommonData<>();
        /*Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        if(StringUtil.isEmpty(user.getUsername())){
            return BaseRespVo.fail(Constant.INVALID_LOGIN_STATE);
        }*/
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
        try {
            result = wxOrderModuleZYService.orderList(parama, userId/*user.getId()*/);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.exception(Constant.DB_EXCEPTION);
        }
        return BaseRespVo.success(result);
    }

    @RequestMapping("detail")
    /**
     * http://182.92.235.201:8083/wx/order/detail?orderId=427 GET
     */
    public BaseRespVo orderDetail(Integer orderId) {
        WXOrderDetailVO result = new WXOrderDetailVO();
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
        try {
            result = wxOrderModuleZYService.orderDetail(orderId, userId);
        } catch (Exception e) {
            return BaseRespVo.exception(Constant.DB_EXCEPTION);
        }
        return BaseRespVo.success(result);
    }

    @RequestMapping("submit")
    /**
     * http://182.92.235.201:8083/wx/order/submit  POST
     * 提交订单，
     * 功能：生成订单，同时修改coupon_user表的该优惠卷状态为已经使用，插入order、order_goods表
     * 订单状态为 未付款
     */
    public BaseRespVo orderSubmit(@RequestBody WXOrderSubmitBO param, HttpSession session) {
        //session.getAttribute("cartIdList")
        //session.getAttribute("userId")
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
        List<Integer> cartIdList = (List<Integer>) subject.getSession().getAttribute("cartid");
        /*List<Integer> cartIdList = new ArrayList<>();
        cartIdList.add(1);
        cartIdList.add(2);
        cartIdList.add(3);
        Integer userId = 1;*/

        WXOrderSubmitVO result;
        try {
            result = wxOrderModuleZYService.orderSubmit(param, cartIdList, userId);
        } catch (Exception e) {
            return BaseRespVo.exception(Constant.DB_EXCEPTION);
        }

        return BaseRespVo.success(result);
    }

    @RequestMapping("prepay")
    /**
     * http://localhost:8083/wx/order/prepay POST
     */
    public ZYBaseVO orderPrepay(@RequestBody Map map) {

        Object orderId = map.get("orderId");
        int id = -1;
        // TODO 他喵的，传过来的orderId，时而字符串，时而整型
        if (!(orderId instanceof Integer)) {
            id = Integer.parseInt((String) orderId);
        }else {
            id = (int) orderId;
        }
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
        try {
            wxOrderModuleZYService.orderPrepay(id, userId);
        } catch (Exception e) {
            return ZYBaseVO.exception(Constant.DB_EXCEPTION);
        }
        return ZYBaseVO.success();
    }

    @RequestMapping("goods")
    /**
     * http://182.92.235.201:8083/wx/order/goods?orderId=504&goodsId=1181000 GET
     *
     */
    public BaseRespVo orderGoods(Integer orderId, Integer goodsId) {

        MarketOrderGoods marketOrderGoods;
        try {
            marketOrderGoods = wxOrderModuleZYService.orderGoods(orderId, goodsId);
        } catch (Exception e) {
            return BaseRespVo.exception(Constant.DB_EXCEPTION);
        }
        return BaseRespVo.success(marketOrderGoods);
    }

    @RequestMapping("comment")
    /**
     * 因为当你可以评论的时候，一定是第一次评论，不需要做判断，数据库表的备注，是说给前端听的
     * http://182.92.235.201:8083/wx/order/comment POST
     */
    public BaseRespVo orderComment(@RequestBody WXOrderCommentBO param) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
        Integer code = 0;
        try {
            code = wxOrderModuleZYService.orderComment(param,userId);
        }catch (Exception e){
            e.printStackTrace();
            return BaseRespVo.exception(Constant.DB_EXCEPTION);
        }
        if(code.equals(Constant.SUCCESS_CODE)){
            return BaseRespVo.success();
        }

        return BaseRespVo.exception("已经评价过了，不需要再次评价");
    }

    @RequestMapping("confirm")
    /**
     * http://182.92.235.201:8083/wx/order/confirm POST
     */
    public BaseRespVo orderConfirm(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        try {
            wxOrderModuleZYService.orderConfirm(orderId);
        } catch (Exception e) {
            return BaseRespVo.exception(Constant.DB_EXCEPTION);
        }
        return BaseRespVo.success();
    }

    @RequestMapping("refund")
    /**
     * http://182.92.235.201:8083/wx/order/refund  POST
     * {orderId: 562}
     */
    public BaseRespVo orderRefund(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        try {
            wxOrderModuleZYService.orderRefund(orderId);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.exception(Constant.DB_EXCEPTION);
        }
        return BaseRespVo.success();
    }

    @RequestMapping("cancel")
    public BaseRespVo orderCancel(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        try {
            wxOrderModuleZYService.orderCancel(orderId);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.exception(Constant.DB_EXCEPTION);
        }
        return BaseRespVo.success();
    }
    @RequestMapping("delete")
    public BaseRespVo orderDelete(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        try {
            wxOrderModuleZYService.orderDelete(orderId);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.exception(Constant.DB_EXCEPTION);
        }
        return BaseRespVo.success();
    }



}
